package com.example.boot1.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//代理对象
public class BigStarInvocationHandler implements InvocationHandler {

    //持有被代理对象
    private Star star;

    public BigStarInvocationHandler(Star star) {
        this.star = star;
    }

    //前置增强
    public void beforeDoSth(){
        System.out.println("----- before -----");
    }

    //后置增强
    public void afterDoSth(){
        System.out.println("----- after -----");
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        beforeDoSth();
        Object o = method.invoke(star,args);
        afterDoSth();
        return o;
    }

    public Object getProxyInstance(){
        return Proxy.newProxyInstance(star.getClass().getClassLoader(),star.getClass().getInterfaces(),this);
    }
}
